package com.ruoyi.project.paea.signature.controller;

import java.io.File;
import java.io.IOException;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.project.paea.signaturelog.domain.PaeaParaRizedSignatureLog;
import com.ruoyi.project.paea.signaturelog.service.IPaeaParaRizedSignatureLogService;
import com.ruoyi.project.param.paeaParaSystem.service.IPaeaParaSystemService;
import com.ruoyi.project.tool.DateTimeUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.paea.signature.domain.PaeaParaAuthorizedSignature;
import com.ruoyi.project.paea.signature.service.IPaeaParaAuthorizedSignatureService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

import com.ruoyi.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;

/**
 * 有权人签字管理Controller
 *
 * @author shangkun.cui
 * @date 2020-04-22
 */
@Controller
@RequestMapping("/paea/signature")
public class PaeaParaAuthorizedSignatureController extends BaseController
{
    private String prefix = "paea/signature";

    @Autowired
    private IPaeaParaAuthorizedSignatureService paeaParaAuthorizedSignatureService;
    @Autowired
    private IPaeaParaRizedSignatureLogService paeaParaRizedSignatureLogService;
    @RequiresPermissions("paea:signature:view")
    @GetMapping()
    public String signature()
    {
        return prefix + "/signature";
    }
    /**
     * 查询有权人签字管理列表
     */
    @RequiresPermissions("paea:signature:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PaeaParaAuthorizedSignature paeaParaAuthorizedSignature)
    {
        startPage();
        List<PaeaParaAuthorizedSignature> list = paeaParaAuthorizedSignatureService.selectPaeaParaAuthorizedSignatureList(paeaParaAuthorizedSignature);
        return getDataTable(list);
    }
    /**
     * 查询生效的有权人签字 单个 直接返回详情页面
     */
    @RequiresPermissions("paea:signature:querySingature")
    @GetMapping("/querySingature")
    public String querySingature(PaeaParaAuthorizedSignature paeaParaAuthorizedSignature,ModelMap mmp)
    {
        QueryWrapper queryWrapper = new QueryWrapper<>(paeaParaAuthorizedSignature);
        Date curDate = DateTimeUtil.getDateFormat();
        queryWrapper.eq("status",new Integer(2));
        queryWrapper.gt("start_date",curDate);
        queryWrapper.lt("end_date",curDate);
        PaeaParaAuthorizedSignature  signature= paeaParaAuthorizedSignatureService.getOne(queryWrapper);
        mmp.put("paeaParaAuthorizedSignature", paeaParaAuthorizedSignature);
        //初始化图片路径
        String _signaturePath = signature.getSignaturePath();
        if(StringUtils.isNotEmpty(_signaturePath)){
            _signaturePath = _signaturePath.replace(RuoYiConfig.getProfile(),"");
            String url = ServerConfig.getDomain(ServletUtils.getRequest());
            _signaturePath = url+Constants.RESOURCE_PREFIX+_signaturePath;
            mmp.put("_signaturePath", _signaturePath);
        }
        return prefix + "/view";
    }
    /**
     * 查询生效的有权人签字 根据机构 返回列表
     */
    @RequiresPermissions("paea:signature:querySingatureByOrg")
    @GetMapping("/querySingatureByOrg")
    public String querySingatureByOrg(PaeaParaAuthorizedSignature paeaParaAuthorizedSignature,ModelMap mmp)
    {
        mmp.put("paeaParaAuthorizedSignature", paeaParaAuthorizedSignature);
        return prefix + "/signatureViewList";
    }
    /**
     * 查询生效的有权人签字列表
     */
    @RequiresPermissions("paea:signature:querySingatures")
    @PostMapping("/querySingatures")
    @ResponseBody
    public TableDataInfo querySingatures(PaeaParaAuthorizedSignature paeaParaAuthorizedSignature)
    {
        startPage();
        QueryWrapper queryWrapper = new QueryWrapper<>(paeaParaAuthorizedSignature);
        Date curDate = DateTimeUtil.getDateFormat();
        queryWrapper.eq("status",new Integer(2));
        queryWrapper.gt("start_date",curDate);
        queryWrapper.lt("end_date",curDate);
        List<PaeaParaAuthorizedSignature> list = paeaParaAuthorizedSignatureService.list(queryWrapper);
        return getDataTable(list);
    }
    /**
     * 导出有权人签字管理列表
     */
    @RequiresPermissions("paea:signature:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PaeaParaAuthorizedSignature paeaParaAuthorizedSignature)
    {
        List<PaeaParaAuthorizedSignature> list = paeaParaAuthorizedSignatureService.list(new QueryWrapper<>(paeaParaAuthorizedSignature));
        ExcelUtil<PaeaParaAuthorizedSignature> util = new ExcelUtil<PaeaParaAuthorizedSignature>(PaeaParaAuthorizedSignature.class);
        return util.exportExcel(list, "signature");
    }

    /**
     * 新增有权人签字管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存有权人签字管理
     */
    @RequiresPermissions("paea:signature:add")
    @Log(title = "有权人签字管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PaeaParaAuthorizedSignature paeaParaAuthorizedSignature,@RequestParam("signature") MultipartFile file)
    {
        return toAjax(paeaParaAuthorizedSignatureService.saveSingature(paeaParaAuthorizedSignature,file));

    }

    /**
     * 修改有权人签字管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        PaeaParaAuthorizedSignature paeaParaAuthorizedSignature = paeaParaAuthorizedSignatureService.getById(id);
        mmap.put("paeaParaAuthorizedSignature", paeaParaAuthorizedSignature);
        //初始化图片路径
        String _signaturePath = paeaParaAuthorizedSignature.getSignaturePath();
        if(StringUtils.isNotEmpty(_signaturePath)){
            _signaturePath = _signaturePath.replace(RuoYiConfig.getProfile(),"");
            String url = ServerConfig.getDomain(ServletUtils.getRequest());
            _signaturePath = url+Constants.RESOURCE_PREFIX+_signaturePath;
            mmap.put("_signaturePath", _signaturePath);
        }
        return prefix + "/edit";
    }
    /**
     * 修改保存有权人签字管理
     */
    @RequiresPermissions("paea:signature:edit")
    @Log(title = "有权人签字管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PaeaParaAuthorizedSignature paeaParaAuthorizedSignature)
    {
        return toAjax(paeaParaAuthorizedSignatureService.updatePaeaParaAuthorizedSignature(paeaParaAuthorizedSignature));
    }
    /**
     * 修改保存有权人签字管理
     */
    @RequiresPermissions("paea:signature:edit")
    @Log(title = "有权人签字管理", businessType = BusinessType.UPDATE)
    @PostMapping("/updateSingature")
    @ResponseBody
    public AjaxResult updateSingature(PaeaParaAuthorizedSignature paeaParaAuthorizedSignature,@RequestParam("signature") MultipartFile file)
    {
        return toAjax(paeaParaAuthorizedSignatureService.updateSingature(paeaParaAuthorizedSignature,file));
    }
    /**
     * 查看有权人签字管理
     */
    @GetMapping("/view/{id}")
    public String view(@PathVariable("id") String id, ModelMap mmap)
    {
        PaeaParaAuthorizedSignature paeaParaAuthorizedSignature = paeaParaAuthorizedSignatureService.getById(id);
        mmap.put("paeaParaAuthorizedSignature", paeaParaAuthorizedSignature);
        //初始化图片路径
        String _signaturePath = paeaParaAuthorizedSignature.getSignaturePath();
        if(StringUtils.isNotEmpty(_signaturePath)){
            _signaturePath = _signaturePath.replace(RuoYiConfig.getProfile(),"");
            String url = ServerConfig.getDomain(ServletUtils.getRequest());
            _signaturePath = url+Constants.RESOURCE_PREFIX+_signaturePath;
            mmap.put("_signaturePath", _signaturePath);
        }
        return prefix + "/view";
    }
    /**
     * 审批有人签字管理
     */
    @GetMapping("/appro/{id}")
    public String appro(@PathVariable("id") String id, ModelMap mmap)
    {
        PaeaParaAuthorizedSignature paeaParaAuthorizedSignature = paeaParaAuthorizedSignatureService.getById(id);
        mmap.put("paeaParaAuthorizedSignature", paeaParaAuthorizedSignature);
        //初始化图片路径
        String _signaturePath = paeaParaAuthorizedSignature.getSignaturePath();
        if(StringUtils.isNotEmpty(_signaturePath)){
            _signaturePath = _signaturePath.replace(RuoYiConfig.getProfile(),"");
            String url = ServerConfig.getDomain(ServletUtils.getRequest());
            _signaturePath = url+Constants.RESOURCE_PREFIX+_signaturePath;
            mmap.put("_signaturePath", _signaturePath);
        }
        return prefix + "/appro";
    }
    /**
     * 保存有权人签字审批
     */
    @RequiresPermissions("paea:signature:approSave")
    @Log(title = "有权人签字管理", businessType = BusinessType.UPDATE)
    @PostMapping("/approSave")
    @ResponseBody
    public AjaxResult approSave(String id,String approRemark,String flag)
    {
        return paeaParaAuthorizedSignatureService.approSave(id,approRemark,flag);
    }
    /**
     * 停用有权人签字管理
     */
    @RequiresPermissions("paea:signature:signatureStop")
    @Log(title = "有权人签字管理", businessType = BusinessType.UPDATE)
    @PostMapping("/signatureStop")
    @ResponseBody
    public AjaxResult signatureStop(String id)
    {
        return paeaParaAuthorizedSignatureService.signatureStop(id);
    }
    /**
     * 删除有权人签字管理
     */
    @RequiresPermissions("paea:signature:remove")
    @Log(title = "有权人签字管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    @Transactional
    public AjaxResult remove(String ids)
    {
        List<String> list = Arrays.asList(ids.split(","));
        List<PaeaParaAuthorizedSignature> signatures = (List)paeaParaAuthorizedSignatureService.listByIds(list);
        //删除操作日志
        paeaParaRizedSignatureLogService.deletePaeaParaRizedSignatureLogBySignatureIds(ids);
        //删除有权人信息
        paeaParaAuthorizedSignatureService.removeByIds(list);
        //删除有权人影像文件
        paeaParaAuthorizedSignatureService.removeImg(signatures);
        return toAjax(1);
    }
}
